package ru.spb.xmlreshelper.screen.cdt;

import com.google.common.collect.Lists;
import ru.spb.xmlreshelper.domain.CDTRecord;

import javax.swing.table.AbstractTableModel;
import java.util.List;

/**
 * @author Nikita.Brazhnikov
 */
public class CDTTableModel extends AbstractTableModel {

    private List<CDTRecord> records = Lists.newArrayList();


    @Override
    public int getRowCount() {
        return records.size();
    }

    @Override
    public int getColumnCount() {
        return 4;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        CDTRecord record = records.get(rowIndex);
        switch (columnIndex) {
            case 0:
                return record.getCdt();
            case 1:
                return record.getName();
            case 2:
                return record.getNamespace();
            case 3:
                return record.getType();
            default:
                return null;
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return Long.class;
            default:
                return String.class;
        }
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0:
                return "ID";
            case 1:
                return "Name";
            case 2:
                return "Namespace";
            case 3:
                return "Type";
            default:
                return null;
        }
    }

    public int getColumnWidth(int column) {
        return 15 * getColumnWidthProc(column);
    }

    private int getColumnWidthProc(int column) {
        switch (column) {
            case 0:
                return 3;
            case 1:
                return 20;
            case 2:
                return 20;
            case 3:
                return 10;
            default:
                return 5;
        }
    }

    public void setModelList(List<CDTRecord> list) {
        records = Lists.newArrayList(list);
        fireTableDataChanged();
    }

    public List<CDTRecord> getRecords() {
        return records;
    }
}
